package com.gy.api.pc.region.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gy.api.base.Constants;
import com.gy.api.base.Type;
import com.gy.api.mobile.company.dao.CompanyInfoDao;
import com.gy.api.mobile.company.domain.po.CompanyInfoPO;
import com.gy.api.pc.examine.dao.ExamineDao;
import com.gy.api.pc.examine.dao.ExamineLogDao;
import com.gy.api.pc.examine.domain.po.ExaminePO;
import com.gy.api.pc.examine.domain.vo.ExamineAddVO;
import com.gy.api.pc.examine.domain.vo.ExamineLogAddVO;
import com.gy.api.pc.examine.service.IExamineService;
import com.gy.api.pc.filings.dao.*;
import com.gy.api.pc.filings.domain.po.PatentApplicationAncillaryPO;
import com.gy.api.pc.filings.domain.po.PatentApplicationCompanyPO;
import com.gy.api.pc.filings.domain.po.PatentApplicationPO;
import com.gy.api.pc.filings.domain.vo.*;
import com.gy.api.pc.region.dao.RegionDao;
import com.gy.api.pc.region.domain.vo.Region;
import com.gy.api.pc.region.service.IRegionService;
import com.gy.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
@Slf4j
public class RegionServiceImpl implements IRegionService {
    @Autowired
    RegionDao regionDao;

    @Override
    public List<Region> getChildrenByParentId(Long parentId) {
        return regionDao.selectChildrenByParentId(parentId);
    }

    @Override
    public void importRegionsFromFile(File file) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode rootNode = objectMapper.readTree(file);
        importRegions(rootNode.get("districts"), null);
    }

    private void importRegions(JsonNode districtsNode,Long parentId) {
        if (districtsNode != null && districtsNode.isArray()) {
            for (JsonNode districtNode : districtsNode) {
                // 根据文件内容来解析字段
                Region region = parseRegion(districtNode, parentId);
                regionDao.insertRegion(region);

                // 如果有子区域，递归地导入它们
                if (districtNode.has("districts")) {
                    importRegions(districtNode.get("districts"), region.getId());
                }
            }
        }
    }


    private Region parseRegion(JsonNode node, Long parentId) {
        Region region = new Region();
        region.setCitycode(node.get("citycode").asText(null));
        region.setAdcode(node.get("adcode").asText(null));
        region.setName(node.get("name").asText(null));
        region.setCenter(node.get("center").asText(null));
        region.setLevel(node.get("level").asText(null));
        region.setParentId(parentId);
        return region;
    }
}